//
//  HZL_SecondMainViewController.m
//  B_Project
//
//  Created by 胡佳俊 on 15/11/23.
//  Copyright © 2015年 HZL. All rights reserved.
//

#import "HZL_SecondMainViewController.h"
#import "HZL_FindTableViewCell.h"
#import "HZL_FindSceneTableViewCell.h"
#import "HZL_ZoneTableViewCell.h"
#import "HZL_NetworkingHandle.h"
#import "HZL_ButtonView.h"
#import "HZL_ZoneViewController.h"
#import "HZL_FindModel.h"
#import "HZL_SceneViewController.h"
#import "HZL_AllViewController.h"
#import "RESideMenu.h"
@interface HZL_SecondMainViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *mySceneView;
@property (nonatomic, strong) UIView *typeView;
@property (nonatomic, strong) NSMutableArray *zoneDataArr;
@property (nonatomic, copy) NSString *body;
@end

@implementation HZL_SecondMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-liebiao"] style:UIBarButtonItemStylePlain target:self action:@selector(presentLeftMenuViewController:)];

    [self creatTableView];
    [self getZoneData];
    [self creatSceneView];
    [self creatTypeView];
    
    
}

#pragma mark - 创建tableView
- (void)creatTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -10, VIEW_WIDTH, VIEW_HEIGHT) style:UITableViewStyleGrouped];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    [self.tableView registerClass:[HZL_FindTableViewCell class] forCellReuseIdentifier:@"type"];
    [self.tableView registerClass:[HZL_FindSceneTableViewCell class] forCellReuseIdentifier:@"scene"];
    [self.tableView registerClass:[HZL_ZoneTableViewCell class] forCellReuseIdentifier:@"zone"];
    [self.view addSubview:self.tableView];
}
#pragma mark - 创建场景view
- (void)creatSceneView
{
    self.mySceneView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 150)];
    self.mySceneView.backgroundColor = [UIColor blueColor];
}
#pragma mark - 创建cell0 View
- (void)creatTypeView
{
    self.typeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 90)];
    
    HZL_ButtonView *Delicacies = [[HZL_ButtonView alloc]initWithFrame:CGRectMake(0, 0, VIEW_WIDTH / 2, 45)];
    Delicacies.backgroundColor = [UIColor colorWithRed:0.96 green:0.8 blue:0.19 alpha:1];
    Delicacies.title.text = @"美食佳肴";
    Delicacies.subTitle.text = @"Delicacies";
    Delicacies.img.image = [UIImage imageNamed:@"iconfont-waimai@2x"];
    [Delicacies doTarget:self action:@selector(foodAction)];
    [self.typeView addSubview:Delicacies];
    
    HZL_ButtonView *Relaxation = [[HZL_ButtonView alloc]initWithFrame:CGRectMake(VIEW_WIDTH / 2, 0, VIEW_WIDTH / 2, 45)];
    Relaxation.backgroundColor = [UIColor colorWithRed:0.32 green:0.83 blue:0.39 alpha:1];
    Relaxation.subTitle.text = @"Relaxation";
    Relaxation.title.text = @"休闲时光";
    Relaxation.img.image = [UIImage imageNamed:@"iconfont-kafeibei@2x"];
    [Relaxation doTarget:self action:@selector(relaxationAction)];
    [self.typeView addSubview:Relaxation];
    

    HZL_ButtonView *Excursion = [[HZL_ButtonView alloc]initWithFrame:CGRectMake(0, 45, VIEW_WIDTH / 2, 45)];
    Excursion.backgroundColor = [UIColor colorWithRed:0.94 green:0.45 blue:0.3 alpha:1];
    Excursion.subTitle.text = @"Excursion";
    Excursion.title.text = @"周边游玩";
    Excursion.img.image = [UIImage imageNamed:@"iconfont-zhoubianyou@2x"];
    [Excursion doTarget:self action:@selector(excursionAction)];
    [self.typeView addSubview:Excursion];
    
    HZL_ButtonView *All = [[HZL_ButtonView alloc]initWithFrame:CGRectMake(VIEW_WIDTH / 2, 45, VIEW_WIDTH / 2, 45)];
    All.backgroundColor = [UIColor colorWithRed:0.32 green:0.67 blue:0.99 alpha:1];
    All.subTitle.text = @"All";
    All.title.text = @"全部";
    All.img.image = [UIImage imageNamed:@"iconfont-quanbu-2@2x"];
    [All doTarget:self action:@selector(allAction)];
    [self.typeView addSubview:All];
}
#pragma mark - 点击cell0方法
- (void)foodAction
{
    HZL_SceneViewController *sVC = [[HZL_SceneViewController alloc] init];
    sVC.tongue = @"tongue";
    sVC.hidesBottomBarWhenPushed = YES;

    [self.navigationController pushViewController:sVC animated:YES];
}
- (void)relaxationAction
{
    HZL_SceneViewController *sVC = [[HZL_SceneViewController alloc] init];
    sVC.tastebar = @"tastebar";
    sVC.hidesBottomBarWhenPushed = YES;

    [self.navigationController pushViewController:sVC animated:YES];
}
- (void)excursionAction
{
    HZL_ZoneViewController *zVC = [[HZL_ZoneViewController alloc] init];
    zVC.hidesBottomBarWhenPushed = YES;

    [self.navigationController pushViewController:zVC animated:YES];
}
- (void)allAction
{
    HZL_AllViewController *aVC = [[HZL_AllViewController alloc] init];
    aVC.hidesBottomBarWhenPushed = YES;

    [self.navigationController pushViewController:aVC animated:YES];
}

- (void)getZoneData
{
    NSString *bodyy = [NSString stringWithFormat:@"cityId=%@", self.body];
    if (self.body == nil) {
        bodyy = @"cityId=xiamen";
    }
    // 商圈
    [HZL_NetworkingHandle requestPOSTWithURl:@"http://www.molyo.com/mShop/bdistrict/getBusinessDistrictList" body:bodyy completion:^(id result) {
        NSMutableArray *tempArr = result[@"body"][@"list"];
        self.zoneDataArr = [HZL_FindModel transformWithArray:tempArr];
        [self.tableView reloadData];
    }];
//    [self creatTableView];
}
- (void)changeCity:(NSNotification *)note
{
    self.body = note.object;
    [self getZoneData];
}
#pragma mark - tableView方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 2) {
        return self.zoneDataArr.count;
    }else {
        return 1;
    }
}
// section高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
// 自定义分组
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return @"";
    } if (section == 1) {
        return @"热门场景";
    } else {
        return @"热门商圈";
    }
}
// 自定义分组
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    // 创建一个视图（v_headerView）
    UIView *v_headerView = [[UIView alloc] initWithFrame:CGRectMake(0, -20, VIEW_WIDTH, 30)];
    //创建一个UILable（v_headerLab）用来显示标题
    if (!(section == 0)) {
        UILabel *locationLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, -20, 100, 25)];
//        locationLabel.textAlignment = NSTextAlignmentCenter;
        locationLabel.font = [UIFont fontWithName:@"Arial" size:16];
        locationLabel.font = [UIFont boldSystemFontOfSize:15];
        locationLabel.shadowColor = [UIColor whiteColor];
        [locationLabel setShadowOffset:CGSizeMake(0, 1)];
        [v_headerView addSubview:locationLabel];
        if (section == 1) {
            locationLabel.text = @"热门场景" ;
        } if (section == 2) {
            locationLabel.text = @"热门商圈";
        }
        
        UILabel *red = [[UILabel alloc] initWithFrame:CGRectMake(10, -17, 5, 20)];
        red.backgroundColor = [UIColor redColor];
        [v_headerView addSubview:red];
        
        UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(15, 9, VIEW_WIDTH - 30, 1)];
        line.backgroundColor = [UIColor grayColor];
        [v_headerView addSubview:line];
        
    }
    
    return v_headerView;
}

#pragma mark - tableView赋值方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 分类
    if (indexPath.section == 0) {
        static NSString *identifier = @"cell1";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell){
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            [cell.contentView addSubview:self.typeView];
        }
        return cell;
    } // 场景
    if (indexPath.section == 1) {
        
        HZL_FindSceneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"scene"];
//        cell.data = self.sceneDataArr;
        return cell;
    } // 商圈
    else {
        HZL_ZoneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"zone"];
        cell.model = self.zoneDataArr[indexPath.row];
        
        return cell;
    }
}

#pragma mark - tableView点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 2) {
        HZL_ZoneViewController *zone = [[HZL_ZoneViewController alloc] init];
        zone.businessDistrictId = [self.zoneDataArr[indexPath.row] iD];
        zone.hidesBottomBarWhenPushed = YES;
        
        [self.navigationController pushViewController:zone animated:YES];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 100;
    } if (indexPath.section == 1) {
        return 110;
    } else {
        return ([UIScreen mainScreen].bounds.size.height - 64 - 49) / 4.2;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
